const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    drugName: '',
    userId:wx.getStorageSync('user').userId,
  },

  //用户对药品进行收藏
  collectVideo:function(e){
    console.log('收藏drug')
    console.log(app.collectdrugId)
    if(this.data.collectImg){
      // console.log(e)
      //如果用户已经点击过收藏图标则将其删除
      console.log(this.data.collectImg)
      this.deleteCollectVideo(e)
    }else{
      console.log(e)
      console.log(this.data.collectImg)
      this.addCollectVideo(e)
    }
    
    
  },

  //取消用户对该药品的收藏
  deleteCollectVideo:function(e){
    console.log("取消收藏")
    console.log(e)
    wx.request({
      url:'http://localhost:3000/collect/collections',
      method:'DELETE',
      data:{
        drugId:e.currentTarget.id,
        drugName:this.drugName,
        userId:wx.getStorageSync('user').userId
      },
      header:{'content-type':'application/json'},
      // dataType:'text',
      success:res=>{
        console.log('删除收藏 success')
        if(res.statusCode==200){
          // console.log('delete success')
          this.setData({collectImg:false});
        }
      }
    })
  },

  //添加用户对该药品的收藏
  addCollectVideo:function(e){
    console.log("取消收藏")
    console.log("drugId:"+e.currentTarget.id)
    wx.request({
      url:'http://localhost:3000/collect/collections',
      method:'POST',
      data:{
        drugId: e.currentTarget.id,
        drugName:this.drugName,
        userId:wx.getStorageSync('user').userId
      },
      header:{'content-type':'application/json'},
      
      // dataType:'text',
      success:res=>{
        if(res.statusCode==200){
          console.log('收藏成功')
          this.setData({collectImg:true});
          
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  collectdrugId:[],
  
  onLoad: function () {
    // 从主页搜索进入
    // if(!app.mylistId){
      console.log(app.drugName)
      wx.request({
        url:`http://localhost:3000/home/drug/list?drugName=${app.drugName}`,
        method:'GET',
        success:res=>{
          if(res.statusCode==200){
            console.log(res.data);
            this.setData({
              drugId: res.data[0].drugId,
              drugName: res.data[0].drugName,
              drugImg: res.data[0].drugImg,
              drugBrand: res.data[0].drugBrand,
              producer: res.data[0].producer,
              composition: res.data[0].composition,
              indication: res.data[0].indication,
              usageMethod: res.data[0].usageMethod,
              pharmacologicalEffect: res.data[0].pharmacologicalEffect,
              mattersNeedingAttention: res.data[0].mattersNeedingAttention
            })
          }
        }
      })

      let userId = wx.getStorageSync('user').userId;
      let len = 0;
      wx.request({
      url: `http://localhost:3000/collect/list?userId=${userId}`,
      method:'GET',
      success: res=>{
        console.log(res.data)
        for(let i=0; i<res.data.length; i++){
          let item= res.data[i].drugId
          this.collectdrugId.push(item)
        }
        app.collectdrugId = this.collectdrugId
        len = app.collectdrugId.length
        // console.log(len)
       
        for(let i=0;i<len;i++){
          if(this.data.drugId == app.collectdrugId[i]){
            this.setData({collectImg:true});
            console.log(this.data.collectImg)
            break;
            
          }
          else{
            this.setData({collectImg:false})
          }
        }
      }
    })
      
  
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})